/**
 * Created by nolan on 15/12/3.
 */
angular.module('sbAdminApp')
    .service('httpResource', ['$rootScope', '$http', '$q', 'contextpath', '$log',  function ($rootScope, $http, $q, contextpath, $log) {
        delete $http.defaults.headers.common['X-Requested-With'];
        return {
            post: function (url, params) {
                url = contextpath + url;
                var deferred = $q.defer();
                $log.info(params);
                return $http.post(url, params)
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (error) {
                        deferred.reject(error);
                    });
                return deferred.promise;
            },

            get: function (url) {
                url = contextpath + url;
                var deferred = $q.defer();
                return $http.get(url)
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (error) {
                        deferred.reject(error);
                    });
                return deferred.promise;
            },

            put: function (url, params) {
                url = contextpath + url;
                var deferred = $q.defer();
                return $http.put(url, params)
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (error) {
                        deferred.reject(error);
                    });
                return deferred.promise;
            },

            delete: function (url) {
                url = contextpath + url;
                var deferred = $q.defer();
                return $http.delete(url)
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (error) {
                        deferred.reject(error);
                    });
                return deferred.promise;
            }
        };
    }]);